package com.itheima.redbaby.activity;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.itheima.redbaby.BaseActivity;
import com.itheima.redbaby.GloableParams;
import com.itheima.redbaby.R;
import com.itheima.redbaby.engine.SearchEngine;
import com.itheima.redbaby.factory.DaoAndEngineFactory;

public class SearchListview extends BaseActivity {
	private List<String> list_recommends = new ArrayList<String>();
	private List<String> list_title;
	private Button bt_ss;
	private ExpandableListView lv_product;
	private CommonNumberAdapter adapter;
	private EditText search_key;
	private boolean[] expanded = {false, false};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.searchactivity);
		
		init();
		getdata();
	}
	
	private void init() {
		bt_ss = (Button) findViewById(R.id.bt_ss);
		lv_product = (ExpandableListView) findViewById(R.id.Lv_product);
		search_key = (EditText) findViewById(R.id.editSearchInfo);
		
		list_title = new ArrayList<String>();
		list_title.add("热门搜索");
		list_title.add("搜索历史");
		
		adapter = new CommonNumberAdapter();
		lv_product.setAdapter(adapter);
		bt_ss.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				String product_name = search_key.getText().toString().trim();
				if("".equals(product_name))
					return;
				
				GloableParams.list_history.add(product_name);
				Intent intent = new Intent(SearchListview.this, SearchResultActivity.class);
				intent.putExtra("product", product_name);
				startActivity(intent);
			}
		});
		lv_product.setOnGroupClickListener(new OnGroupClickListener() {
			@Override
			public boolean onGroupClick(ExpandableListView parent, View v,
					int groupPosition, long id) {
				//TODO -111-
//				ImageView iv = (ImageView) v.getTag();
//				if(groupPosition == 0) {
//					expanded0 = !expanded0;
//					System.out.println(expanded0 ? "0展开了" : "0关闭了");
//					iv.setBackgroundResource(expanded0 ? R.drawable.arrow1 : R.drawable.arrow);
//				} else if(groupPosition == 1){
//					expanded1 = !expanded1;
//					System.out.println(expanded1 ? "1展开了" : "1关闭了");
//					iv.setBackgroundResource(expanded1 ? R.drawable.arrow1 : R.drawable.arrow);
//				}
				return false;
			}
		});
		lv_product.setOnChildClickListener(new OnChildClickListener() {
			@Override
			public boolean onChildClick(ExpandableListView parent, View v,
					int groupPosition, int childPosition, long id) {
				String keyword = "";
				if(groupPosition == 0) {
					keyword = list_recommends.get(childPosition);
				} else if(groupPosition == 1) {
					keyword = GloableParams.list_history.get(childPosition);
				}
				
				Intent intent = new Intent(SearchListview.this, SearchResultActivity.class);
				intent.putExtra("product", keyword);
				startActivity(intent);
				return true;
			}
		});
	}
	
	private void getdata() {
		new MyHttpTask() {
			@Override
			protected Object doInBackground(Void... params) {
				SearchEngine searchEngine = DaoAndEngineFactory.getImpl(SearchEngine.class);
				List<String> list = searchEngine.getRecommendSearch();
				return list;
			}
			@Override
			@SuppressWarnings("unchecked")
			protected void onPostExecute(Object result) {
				List<String> list = (List<String>) result;
				list_recommends.addAll(list);
				
				adapter.notifyDataSetChanged();
			}
		}.executeHttp();
	}
	
	private class CommonNumberAdapter extends BaseExpandableListAdapter {
		@Override
		public void onGroupExpanded(int groupPosition) {
			expanded[groupPosition] = true;
		}
		@Override
		public void onGroupCollapsed(int groupPosition) {
			expanded[groupPosition] = false;
		}
		@Override
		public int getGroupCount() {
			if(list_title == null)
				return 0;
			return list_title.size();
		}
		@Override
		public int getChildrenCount(int groupPosition) {
			if(groupPosition == 1)
				return GloableParams.list_history.size();
			if(list_recommends == null)
				return 0;
			return list_recommends.size();
		}
		@Override
		public Object getGroup(int groupPosition) {
			return null;
		}
		@Override
		public Object getChild(int groupPosition, int childPosition) {
			return null;
		}
		@Override
		public long getGroupId(int groupPosition) {
			return groupPosition;
		}
		@Override
		public long getChildId(int groupPosition, int childPosition) {
			return childPosition;
		}
		@Override
		public boolean hasStableIds() {
			return false;
		}
		@Override
		public View getGroupView(int groupPosition, boolean isExpanded,
				View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = View.inflate(SearchListview.this, R.layout.item_expandlist_group, null);
			}
			ImageView iv_arrow = (ImageView) convertView.findViewById(R.id.iv_arrow);
			TextView group_content = (TextView) convertView.findViewById(R.id.tv_group_content);
			group_content.setTextColor(Color.RED);
			group_content.setTextSize(20);
			group_content.setText(list_title.get(groupPosition));
			if (expanded[groupPosition]) {
				iv_arrow.setBackgroundResource(R.drawable.arrow1);
			} else {
				iv_arrow.setBackgroundResource(R.drawable.arrow);
			}
			
			return convertView;
		}
		@Override
		public View getChildView(int groupPosition, int childPosition,
				boolean isLastChild, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = View.inflate(SearchListview.this, R.layout.item_expandlist_child, null);
				TextView child_content = (TextView) convertView.findViewById(R.id.tv_child_content);
				convertView.setTag(child_content);
			}
			TextView child_content = (TextView) convertView.getTag();
			child_content.setTextColor(Color.BLACK);
			child_content.setTextSize(16);
			child_content.setText(list_title.get(groupPosition));
			if(groupPosition == 0) {
				child_content.setText(list_recommends.get(childPosition));
			} else {
				child_content.setText(GloableParams.list_history.get(childPosition));
			}
			
			return convertView;
		}
		@Override
		public boolean isChildSelectable(int groupPosition, int childPosition) {
			return true;
		}
	}
}
